// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   applyPayExpenses_draft_ok.java

package draft.fund;

import include.nseer_cookie.counter;
import include.nseer_cookie.exchange;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.*;

public class applyPayExpenses_draft_ok extends HttpServlet
{

	ServletContext application;
	HttpSession session;

	public applyPayExpenses_draft_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		ServletContext dbApplication = dbSession.getServletContext();
		try
		{
			HttpSession session = request.getSession();
			java.io.PrintWriter out = response.getWriter();
			nseer_db_backup1 fund_db = new nseer_db_backup1(dbApplication);
			nseer_db_backup1 fund_db1 = new nseer_db_backup1(dbApplication);
			if (fund_db.conn((String)dbSession.getAttribute("unit_db_name")) && fund_db1.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				counter count = new counter(dbApplication);
				ValidataRecordNumber vrn = new ValidataRecordNumber();
				ValidataTag vt = new ValidataTag();
				ValidataNumber validata = new ValidataNumber();
				try
				{
					String time = "";
					Date now = new Date();
					SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
					time = formatter.format(now);
					String apply_pay_ID = request.getParameter("apply_pay_ID");
					String register_time = request.getParameter("register_time");
					String register = request.getParameter("register");
					String register_ID = request.getParameter("register_ID");
					String bodyc = new String(request.getParameter("remark").getBytes("UTF-8"), "UTF-8");
					String remark = exchange.toHtml(bodyc);
					String amount = request.getParameter("amount");
					String file_kind[] = request.getParameterValues("file_kind");
					String cost_price_subtotal[] = request.getParameterValues("cost_price_subtotal");
					int p = 0;
					String file_kinda = ",";
					for (int j = 1; j < file_kind.length; j++)
					{
						file_kinda = (new StringBuilder(String.valueOf(file_kinda))).append(file_kind[j]).append(",").toString();
						StringTokenizer tokenTO4 = new StringTokenizer(cost_price_subtotal[j], ",");
						String cost_price_subtotal1;
						for (cost_price_subtotal1 = ""; tokenTO4.hasMoreTokens(); cost_price_subtotal1 = (new StringBuilder(String.valueOf(cost_price_subtotal1))).append(tokenTO4.nextToken()).toString());
						if (cost_price_subtotal1.equals("") || !validata.validata(cost_price_subtotal1))
							p++;
					}

					int n = 0;
					for (int i = 1; i <= Integer.parseInt(amount); i++)
					{
						String tem_file_kind = (new StringBuilder("file_kind")).append(i).toString();
						String tem_cost_price_subtotal = (new StringBuilder("cost_price_subtotal")).append(i).toString();
						String cost_price_subtotal2 = request.getParameter(tem_cost_price_subtotal);
						String file_kind2 = request.getParameter(tem_file_kind);
						if (file_kinda.indexOf(file_kind2) != -1)
							n++;
						if (cost_price_subtotal2.equals("") || !validata.validata(cost_price_subtotal2))
							p++;
					}

					if (Integer.parseInt(amount) == 0 && file_kind.length == 1)
						response.sendRedirect((new StringBuilder("draft/fund/applyPayExpenses_ok_a.jsp?apply_pay_ID=")).append(apply_pay_ID).toString());
					else
					if (n == 0)
					{
						if (p == 0)
						{
							if (vt.validata((String)dbSession.getAttribute("unit_db_name"), "fund_apply_pay", "apply_pay_ID", apply_pay_ID, "check_tag").equals("5") || vt.validata((String)dbSession.getAttribute("unit_db_name"), "fund_apply_pay", "apply_pay_ID", apply_pay_ID, "check_tag").equals("9"))
							{
								String currency_name = "";
								String personal_unit = "";
								String chain_ID = "";
								String chain_name = "";
								String funder = "";
								String funder_ID = "";
								String sql11 = (new StringBuilder("select * from fund_apply_pay where apply_pay_ID='")).append(apply_pay_ID).append("'").toString();
								for (ResultSet rs11 = fund_db.executeQuery(sql11); rs11.next();)
								{
									chain_ID = rs11.getString("chain_ID");
									chain_name = rs11.getString("chain_name");
									funder = rs11.getString("human_name");
									funder_ID = rs11.getString("human_ID");
									currency_name = rs11.getString("currency_name");
									personal_unit = rs11.getString("personal_unit");
								}

								int expenses_amount = 0;
								String sql6 = (new StringBuilder("select count(*) from fund_apply_pay_details where apply_pay_ID='")).append(apply_pay_ID).append("'").toString();
								ResultSet rs6 = fund_db.executeQuery(sql6);
								if (rs6.next())
									expenses_amount = rs6.getInt("count(*)");
								double demand_cost_price_sum = 0.0D;
								for (int i = 1; i <= expenses_amount; i++)
								{
									String tem_cost_price_subtotal = (new StringBuilder("cost_price_subtotal")).append(i).toString();
									String cost_price_subtotal2 = request.getParameter(tem_cost_price_subtotal);
									demand_cost_price_sum += Double.parseDouble(cost_price_subtotal2);
									sql6 = (new StringBuilder("update fund_apply_pay_details set cost_price_subtotal='")).append(cost_price_subtotal2).append("' where apply_pay_ID='").append(apply_pay_ID).append("' and details_number='").append(i).append("'").toString();
									fund_db.executeUpdate(sql6);
								}

								for (int i = 1; i < file_kind.length; i++)
								{
									StringTokenizer tokenTO1 = new StringTokenizer(file_kind[i], "/");
									String file_chain_ID = "";
									String file_chain_name;
									for (file_chain_name = ""; tokenTO1.hasMoreTokens(); file_chain_name = tokenTO1.nextToken())
										file_chain_ID = tokenTO1.nextToken();

									StringTokenizer tokenTO4 = new StringTokenizer(cost_price_subtotal[i], ",");
									String cost_price_subtotal1;
									for (cost_price_subtotal1 = ""; tokenTO4.hasMoreTokens(); cost_price_subtotal1 = (new StringBuilder(String.valueOf(cost_price_subtotal1))).append(tokenTO4.nextToken()).toString());
									demand_cost_price_sum += Double.parseDouble(cost_price_subtotal1);
									expenses_amount++;
									String sql1 = (new StringBuilder("insert into fund_apply_pay_details(apply_pay_ID,details_number,file_chain_ID,file_chain_name,cost_price_subtotal) values ('")).append(apply_pay_ID).append("','").append(expenses_amount).append("','").append(file_chain_ID).append("','").append(file_chain_name).append("','").append(cost_price_subtotal1).append("')").toString();
									fund_db.executeUpdate(sql1);
								}

								String sql = (new StringBuilder("update fund_apply_pay set demand_cost_price_sum='")).append(demand_cost_price_sum).append("',register_time='").append(register_time).append("',register='").append(register).append("',remark='").append(remark).append("' where apply_pay_ID='").append(apply_pay_ID).append("'").toString();
								fund_db.executeUpdate(sql);
								response.sendRedirect("draft/fund/applyPayExpenses_ok.jsp?finished_tag=0");
							} else
							{
								response.sendRedirect("draft/fund/applyPayExpenses_ok.jsp?finished_tag=1");
							}
						} else
						{
							response.sendRedirect("draft/fund/applyPayExpenses_ok.jsp?finished_tag=6");
						}
					} else
					{
						response.sendRedirect("draft/fund/applyPayExpenses_ok.jsp?finished_tag=7");
					}
				}
				catch (Exception ex)
				{
					ex.printStackTrace();
				}
				fund_db.commit();
				fund_db1.commit();
				fund_db.close();
				fund_db1.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
